<template>
  <div class="companyTable">
    <!-- button -->
    <el-button
      type="primary"
      icon="el-icon-plus"
      size="mini"
      style="margin-bottom: 20px; margin-top: 20px"
      @click="addRole"
    >新增角色</el-button>
    <!-- table -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed type="index" label="序号" />
      <el-table-column prop="name" label="角色名" />
      <el-table-column prop="description" label="描述" />
      <el-table-column prop="province" label="操作">
        <template slot-scope="{ row }">
          <el-button
            type="text"
            size="small"
            @click="handleClick(row)"
          >分配权限</el-button>
          <el-button
            type="text"
            size="small"
            @click="editRole(row)"
          >修改</el-button>
          <el-button
            type="text"
            size="small"
            @click="delteRole(row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      small
      layout=" total,sizes, prev,pager,next"
      :total="total"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="pageSize"
      :current-page="page"
      @current-change="currentPage"
      @size-change="handleSizeChange"
    />

    <roleDialog
      ref="roleDialog"
      :visible.sync="isShow"
      :form="form"
      @clear="clear"
      @getRole="getRole"
    />
  </div>
</template>

<script>
import { getRoles, getRolesId, deleteRolesId } from '@/api/setting'
export default {
  components: {
    roleDialog: () => import('@/views/setting/component/roleDialog.vue')
  },
  data() {
    return {
      // 表格数据
      tableData: [],
      // 总条数
      total: 0,
      // 控制页码
      page: 1,
      // 页容量
      pageSize: 2,
      // 控制弹窗
      isShow: false,
      // 表单
      form: this.initForm()
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    // 初始化表单
    initForm() {
      return {
        name: '',
        id: '',
        description: ''
      }
    },
    // 清空弹出框的表单
    clear() {
      this.isShow = false
    },
    // 获取角色列表
    async getRole() {
      const { data: res } = await getRoles({
        page: this.page,
        pagesize: this.pageSize
      })
      this.tableData = res.rows
      this.total = res.total
    },
    // 点击添加按钮
    addRole() {
      // 里面没有id
      Object.assign(this.form, this.initForm())
      this.isShow = true
    },
    // 编辑按钮
    async editRole(row) {
      // 请求数据
      const { data: res } = await getRolesId(row.id)
      // 将数据给form，里面有id
      Object.assign(this.form, res)
      this.isShow = true
    },
    // 删除按钮
    async delteRole(row) {
      this.$confirm('正在删除角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          if (this.tableData.length == 1) {
            this.page > 1 ? --this.page : this.page
          }

          const res = await deleteRolesId(row.id)
          this.$message({
            type: 'success',
            message: res.message
          })
          this.getRole()
        })
        .catch(() => {})
    },
    // 分页器
    currentPage(page) {
      this.page = page
      this.getRole()
    },
    // 页容量
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.page = 1
      this.getRole()
    }
  }
}
</script>

<style lang="scss" scoped>
.companyTable {
  .el-pagination {
    margin-top: 1.666667rem;
    text-align: right;
  }
  ::v-deep .el-select .el-input .el-input__inner {
    height: 100%;
  }
}
</style>
